 /*      smol.l
 *
 *      Copyright 2009-2010 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
  */

%{
#include <stdlib.h>
#include "smol.hh"
#include <string.h>
#include <math.h>

void yyerror(char *);
void count();
extern unsigned int ind;
extern int main_buf;

#define YYSTYPE char *

//char *yylval;

%}

%Start Normal S1

L	[a-zA-Z_]

D	[0-9]

D8	[0-7]

INT	{D}+

INT8	0{D8}+

H	[a-fA-F0-9]

%%

				BEGIN Normal;
				
<Normal>\"				{ BEGIN S1; }
<S1>[^\"]*				{ yylval = strdup(yytext); input(); BEGIN Normal; return (SOME_TEXT);  }
<Normal>"/*"			{ comment(); }
<Normal>"//"			{ comment_line(); }
<Normal>"var"			{ count("var"); return (VAR); }
<Normal>"new"			{ count("new"); return (NEW); }
<Normal>"in"			{ count("in"); return (IN); }
<Normal>"for"			{ count("for"); return (FOR); }
<Normal>"foreach"		{ count("for"); return (FOREACH); }
<Normal>"while"			{ count("while"); return (WHILE); }
<Normal>"if"			{ count("if"); return (IF); }
<Normal>"elif"			{ count("elif"); return (ELIF); }
<Normal>"else"			{ count("else"); return (ELSE); }
<Normal>"put"			{ count("put"); return (PUT); }
<Normal>"puts"			{ count("puts"); return (PUTS); }
<Normal>"input"			{ count("input"); return (INPUT); }
<Normal>"Integer"		{ count("Integer"); return (INTEGER); }
<Normal>"String"		{ count("String"); return (STRING); }
<Normal>"Float"			{ count("Float"); return (FLOAT); }
<Normal>"Boolean"		{ count("Boolean"); return (BOOLEAN); }
<Normal>"Array"			{ count("Array"); return (ARRAY); }
<Normal>"Hash"			{ count("Hash"); return (HASH); }
<Normal>"File"			{ count("File"); return (CFILE); }
<Normal>"true"			{ count("true"); return (BTRUE); }
<Normal>"false"			{ count("false"); return (BFALSE); }
<Normal>"method"		{ count("method"); return (METHOD); }
<Normal>"value"			{ count("value"); return (VALUE); }
<Normal>"this"			{ count("value"); return (THIS); }
<Normal>"delete"		{ count("delete"); return (DELETE); }
<Normal>"break"			{ count("delete"); return (BREAK); }
<Normal>"exit"			{ count("exit"); return (EXIT); }
<Normal>"and"			{ count("and"); return (AND); }
<Normal>"or"			{ count("or"); return (OR); }
<Normal>"not"			{ count("not"); return (NOT); }
<Normal>"xor"			{ count("xor"); return (XOR); }

<Normal>{L}({L}|{D})*	{ count("ID"); yylval = strdup(yytext); return (ID); }

<Normal>0{D8}+			{ count("NUM"); yylval = strdup(yytext); return (NUM); }
<Normal>0[xX]{H}+		{ count("HNUM"); yylval = strdup(yytext); return (HNUM); }
<Normal>{D}*\.{D}+		{ count("FNUM"); yylval = strdup(yytext); return (FNUM); }
<Normal>{D}+			{ count("NUM"); yylval = strdup(yytext); return (NUM); }

<Normal>"+="			{ count("+="); return (ADD_ASSIGN); }
<Normal>"-="			{ count("-="); return (SUB_ASSIGN); }
<Normal>"/="			{ count("/="); return (DIV_ASSIGN); }
<Normal>"*="			{ count("*="); return (MUL_ASSIGN); }
<Normal>"%="			{ count("%="); return (MOD_ASSIGN); }
<Normal>"^="			{ count("^="); return (XOR_ASSIGN); }
<Normal>"|="			{ count("|="); return (OR_ASSIGN); }
<Normal>"&="			{ count("&="); return (AND_ASSIGN); }
<Normal>">>"			{ count(">>"); return (RIGHT_OP); }
<Normal>"<<"			{ count("<<"); return (LEFT_OP); }
<Normal>"++"			{ count("++"); return (INC_OP); }
<Normal>"--"			{ count("--"); return (DEC_OP); }
<Normal>"&&"			{ count("&&"); return (AND_OP); }
<Normal>"||"			{ count("||"); return (OR_OP); }
<Normal>">="			{ count(">="); return (GE_OP); }
<Normal>"<="			{ count("<="); return (LE_OP); }
<Normal>"=="			{ count("=="); return (EQ_OP); }
<Normal>"!="			{ count("!="); return (NE_OP); }
<Normal>"[]"			{ count("[]"); return (DBL_SK); }

<Normal>";"			{ count(";"); return (';'); }
<Normal>"{"			{ count("{"); return ('{'); }
<Normal>"}"			{ count("}"); return ('}'); }
<Normal>","			{ count(","); return (','); }
<Normal>":"			{ count(":"); return (':'); }
<Normal>"="			{ count("="); return ('='); }
<Normal>"("			{ count("("); return ('('); }
<Normal>")"			{ count(")"); return (')'); }
<Normal>"["			{ count("["); return ('['); }
<Normal>"]"			{ count("]"); return (']'); }
<Normal>"."			{ count("."); return ('.'); }
<Normal>"&"			{ count("&"); return ('&'); }
<Normal>"|"			{ count("|"); return ('|'); }
<Normal>"!"			{ count("!"); return ('!'); }
<Normal>"~"			{ count("~"); return ('~'); }
<Normal>"-"			{ count("-"); return ('-'); }
<Normal>"+"			{ count("+"); return ('+'); }
<Normal>"*"			{ count("*"); return ('*'); }
<Normal>"/"			{ count("/"); return ('/'); }
<Normal>"%"			{ count("%"); return ('%'); }
<Normal>"<"			{ count("<"); return ('<'); }
<Normal>">"			{ count(">"); return ('>'); }
<Normal>"^"			{ count("^"); return ('^'); }
<Normal>"?"			{ count("?"); return ('?'); }

<Normal>\n			{ count("\n"); }
<Normal>[ \t\v\f]		{ count("_"); }
.				yyerror("Unknown character\n");

%%
	//	<Normal>[\n]		return ('\n');
int comment() 
{
	char c, c1;
loop:
	while ((c = input()) != '*' && c != 0)
		putchar(c);
	if ((c = input()) != '/' && c != 0)
	{
		unput(c1);
		goto loop;
	}
	
	if (c != 0)
		putchar(c1);
}

int comment_line() 
{
	char c, c1;
loop:
		while ((c = input()) != '\n' && c != 0)
		putchar(c);
	if ((c = input()) != '\n' && c != 0)
	{
		unput(c1);
		goto loop;
	}
	
	if (c != 0)
		putchar(c1);
}

int column = 0;

void count(const char *mes)
{
	//printf("%s", mes);
	if (! main_buf)
		main_buf = yy_create_buffer( yyin, YY_BUF_SIZE );
	int i;
	for(i = 0; yytext[i] != '\0'; i++)
		if (yytext[i] == '\n')
			column = 0;
	else if (yytext[i] == '\t')
		column += 8 - (column % 8);
	else
		column++;
}

int yywrap(void)
{
   return 1;
}

